﻿using System;
using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;

namespace UcAsp.Net.NOSQLDAL
{
    public class CourseDAL : UcAsp.Net.IDAL.ICourseDAL
    {
        public List<Course> GetCourse()
        {
            return STSData.GetInstance.Select<Model.Course>();
        }

        public List<Course> Select(int pagesize, int pageindex, out int pagecount)
        {
            List<Course> list = new List<Course>();
            var query = GetCourse();
            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }

        public Course GetCourseByID(int id)
        {
            Course course = GetCourse().FirstOrDefault(o => o.ID == id);
            return course;
        }

        public List<Course> GetCourseByActionID(int pagesize, int pageindex, out int pagecount, int actionid)
        {
            List<Course> list = new List<Course>();
            var query = GetCourse().Where(o => o.ActionId == actionid);
            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }

        public bool Insert(Course model)
        {
            long key;
            Model.Course lastrow = STSData.GetInstance.LastRow<Model.Course>(out key);
            model.ID = Convert.ToInt32(key.ToString()) + 1;
            STSData.GetInstance.Insert<Model.Course>(model.ID, model);
            return true;
        }

        public bool Update(Course model)
        {
            STSData.GetInstance.Update<Model.Course>(model.ID, model);
            return true;
        }

        public bool Delete(Course model)
        {
            STSData.GetInstance.Delete<Model.Course>(model.ID);
            return true;
        }
    }
}